package mainframe;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

public class MemTable extends JTable implements MouseListener{
	private static final long serialVersionUID = 1L;
	/**
	 * store the break point
	 */
	private boolean [] breakPoint;
	
	/**
	 * the selected row and column
	 */
	private int curR, curC;
	
	private Icon icon;
	
	private int colorRow;
	
	private Color rowColor;
	
	public boolean[] getBreakPoint(){
		return breakPoint;
	}
	
	MemTable(){
		super();
		this.setModel(new MemTableModel());
		icon = new ImageIcon("breakpoint.jpg");
		if (icon == null)
			System.out.println("Can't find Icon!");
		breakPoint = new boolean[1024];
		for(int i=0; i<1024; i++)
			breakPoint[i] = false;
		
		this.setDefaultRenderer(String.class, new CommonRenderer());
		this.getColumnModel().getColumn(1).setCellRenderer(new BrkPntRenderer());
		this.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		this.setGridColor(Color.lightGray);
		this.setSelectionBackground(Color.white);
		this.getColumnModel().getColumn(0).setPreferredWidth(20);
		this.getColumnModel().getColumn(1).setPreferredWidth(20);
		this.getColumnModel().getColumn(2).setPreferredWidth(60);
		this.getColumnModel().getColumn(3).setPreferredWidth(130);
		this.getColumnModel().getColumn(4).setPreferredWidth(130);
		this.addMouseListener(this);
	}
	
	public void setBackground(int row, Color color){
		colorRow = row;
		rowColor = color;
	}

	
	private class BrkPntRenderer extends DefaultTableCellRenderer{
		private static final long serialVersionUID = 1L;
		
		public Component getTableCellRendererComponent(JTable table, Object value,
				boolean isSelected, boolean hasFocus, int row, int column) {
			// TODO Auto-generated method stub
		        if (breakPoint[row] == true)
		        		setIcon(icon);
		        else
		        		setIcon(null);
			return this;
		}
	}

	private class CommonRenderer extends DefaultTableCellRenderer{
		private static final long serialVersionUID = 1L;

		public Component getTableCellRendererComponent(JTable table, Object value,
				boolean isSelected, boolean hasFocus, int row, int column) {
			// TODO Auto-generated method stub
			super.getTableCellRendererComponent
			(table, value, isSelected, hasFocus, row, column);
			if(colorRow == row)
				this.setBackground(rowColor);
			else
				this.setBackground(Color.white);
			this.setHorizontalAlignment(0);
			return this;
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		curR = this.getSelectedRow();
		curC = this.getSelectedColumn();
		if (curC == 1){
        	if (breakPoint[curR] == false)
        		breakPoint[curR] = true;
        	else
        		breakPoint[curR] = false;
        }
		this.updateUI();
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

}





